Skip to content
This repository has been archived by the owner on Sep 1, 2020. It is now read-only.

Latest commit

 

History

History
43 lines (34 loc) · 1.35 KB

11 - Process/Pool.md

File metadata and controls

43 lines (34 loc) · 1.35 KB

Process\Pool

进程池,基于ServerManager模块实现。可管理多个工作进程。该模块的核心功能为进程管理,相比Process实现多进程,Process\Pool更加简单,封装层次更高,开发者无需编写过多代码即可实现进程管理功能。

此特性需要2.1.2或更高版本

常量定义

  • SWOOLE_IPC_MSGQUEUE :系统消息队列通信
  • SWOOLE_IPC_SOCKETSOCKET通信

异步支持

  • 可在onWorkerStart中使用Swoole提供的异步或协程API,工作进程即可实现异步
  • 底层自带的消息队列和SOCKET通信均为同步阻塞IO
  • 如果进程为异步模式,请勿使用任何自带的同步IPC进程通信功能 (无法使用message回调)

低于4.0版本需要在onWorkerStart末尾添加swoole_event_wait进入事件循环

使用实例

$workerNum = 10;
$pool = new Swoole\Process\Pool($workerNum);

$pool->on("WorkerStart", function ($pool, $workerId) {
    echo "Worker#{$workerId} is started\n";
    $redis = new Redis();
    $redis->pconnect('127.0.0.1', 6379);
    $key = "key1";
    while (true) {
         $msgs = $redis->brpop($key, 2);
         if ( $msgs == null) continue;
         var_dump($msgs);
     }
});

$pool->on("WorkerStop", function ($pool, $workerId) {
    echo "Worker#{$workerId} is stopped\n";
});

$pool->start();